package com.mingmen.supplier.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.StoreInfoAdapter;
import com.mingmen.supplier.adapter.StoreMarketAdapter;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.label.bean.LabelTestBean;
import com.mingmen.supplier.label.recycler.OnItemClickListener;
import com.mingmen.supplier.model.GongYingLabelModel;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.MyStringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class StoreMarketLabelActivity extends AppCompatActivity {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;
    //    中间按钮
    @BindView(R.id.text_center)
    TextView textCenter;
    private ImagePicker imagePicker;

    @BindView(R.id.btn_login)
    Button btn_login;

    private int resurtResurant = 100;
    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;
    private StoreMarketAdapter mAdapter_label;
    private List<GongYingLabelModel.DataBean> mListData;
    List<String> stringList = new ArrayList<>();
    List<String> idSend = new ArrayList<>();
    @BindView(R.id.text_Product)
    TextView text_Product;
    List<String> categoryId = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_storemarket);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();

//        s设置状态栏
        textCenter.setText("供应商经营品类");

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mListData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            stringList.add("ITem" + i);
        }


        mAdapter_label = new StoreMarketAdapter(this, mListData);
        mRecyclerView.setAdapter(mAdapter_label);
        HttpResponse.listMarket(new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("listSupplierCategory", "onSuccess");
                GongYingLabelModel gongYingLabelModel = new GongYingLabelModel();
                gongYingLabelModel = GsonUtil.GsonToBean(s, GongYingLabelModel.class);
                mListData.addAll(gongYingLabelModel.getData());
//                mAdapter_label.update(mListData);
                mAdapter_label.update(mListData);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                Log.d("listSupplierCategory", "onError");
            }
        });


        mAdapter_label.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                addOrRemove(position);
            }

            @Override
            public void OnItemLongClick(View view, int position) {

            }
        });
    }


    @OnClick({R.id.text_return, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
                finish();
                break;
            case R.id.btn_login:
                String mysend = MyStringUtils.returnStringBuffer(idSend);
                Log.d("mysendmysend", mysend);
                String categoryIds = MyStringUtils.returnStringBuffer(categoryId);
             /*   Intent intent = new Intent(StoreMarketLabelActivity.this, StoreInformaticaActivity.class);
                intent.putExtra("companyName",mysend);
                intent.putExtra("companyId", categoryId.toString());
                setResult(200, intent);
                finish();*/
                Intent intent = new Intent(StoreMarketLabelActivity.this, StoreInformaticaActivity.class);
                intent.putExtra("companyName", mysend);
                intent.putExtra("markId", categoryIds);
                setResult(200, intent);
                finish();
                break;
        }
    }


    public static Set<Integer> positionSet = new HashSet<>();
    public Set<String> checkTYpeNameSet = new HashSet<>();
    private boolean selectMode = true; //选择模式 多选或者单选  true  多选
    private String checktypeId = ""; //记录选择种类 多种类以" ,"分开
    public Set<String> checkCheckNameId = new HashSet<>();
    public Set<String> checkTYpeNameId = new HashSet<>();

    private void addOrRemove(int position) {
        if (positionSet.contains(position)) {
            // 如果包含，则撤销选择
            positionSet.remove(position);
//            checkTYpeNameSet.remove(mListData.get(position).getTagName());
            checkTYpeNameSet.remove(mListData.get(position).getName());
            checkTYpeNameId.remove(mListData.get(position).getId());
        } else {
            // 如果不包含，则添加
            positionSet.add(position);
//            checkTYpeNameSet.add(mListData.get(position).getTagName());
            checkTYpeNameSet.add(mListData.get(position).getName());
            checkTYpeNameId.add(mListData.get(position).getId());
        }

        if (positionSet.size() == 0) {
            // 如果没有选中任何的item，则退出多选模式
            mAdapter_label.notifyDataSetChanged();
            selectMode = false;
        } else {
            // 更新列表界面，否则无法显示已选的item
            mAdapter_label.notifyDataSetChanged();
        }
        Log.e("info", positionSet.toString());
        if (idSend.size() > 0) {
            idSend.clear();
        }

        idSend = new ArrayList<>(checkTYpeNameSet);
        categoryId = new ArrayList<>(checkTYpeNameId);
        Log.e("ArraysArraysArrays", idSend.toString());
        Log.e("categoryIdcategoryId", categoryId.toString());
        String myStringBuffer = MyStringUtils.returnStringBuffer(idSend);

        text_Product.setText(myStringBuffer);
//        idSend.add(result.toString());
//        Toast.makeText(SelectGongYingActivity.this, checkTYpeNameSet.toString(), Toast.LENGTH_SHORT).show();
    }
}




